﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;



namespace Webchatting.Dal
{
     public  class OperationFriends
    {
         #region 变量
        public static readonly string connStr = (ConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
        public SqlConnection ConnSql
        {
            set;
            get;
        }
        #endregion
        /// <summary>
        /// 构造函数，用于初始化
        /// </summary>
        public OperationFriends()
        {
            //初始化数据库连接
            this.ConnSql = new SqlConnection(connStr);
        }


        //查找好友
        public string SerachFriend(string serachId, ArrayList arrRUIds, ArrayList arrRUNickNames, ArrayList arrRUPortraits, ArrayList arrRUDess)
        {
            arrRUIds.Clear();
            arrRUNickNames.Clear();
            arrRUPortraits.Clear();
            arrRUDess.Clear();
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border='0px' style='width:100%;border:1px solid #CCCCCC;' cellPadding='0' cellSpacing='2'>");
            sb.Append("<tr><td style='height:25px;background:#CCCCCC'>用户ID</td><td style='height:25px;background:#CCCCCC'>昵称</td></tr>");
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT [RUId],[RUNickName],[RUPortrait],[RUDes] FROM [RUser] WHERE CONVERT(nchar(20),[RUId]) LIKE" + "'%" + serachId + "%'");
            while (dr.Read())
            {
                Encoding edUTF = Encoding.UTF8;
                Encoding edGB = Encoding.GetEncoding("GB2312");
                sb.Append("<tr>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["RUId"].ToString() + "</td>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["RUNickName"].ToString() + "</td>" + "</tr>");
                arrRUIds.Add(dr["RUId"].ToString().Trim());
                arrRUNickNames.Add(dr["RUNickName"].ToString().Trim());
                arrRUPortraits.Add(dr["RUPortrait"].ToString().Trim());
                arrRUDess.Add(dr["RUDes"].ToString().Trim());
            }
            sb.Append("</table>");
            dr.Close();
            ConnSql.Close();
            return sb.ToString();
        }
         
         //添加好友 一方添加 双方都添加成功
        public int AddFriend(string RUId, string FRUId, string FRUNickName,string RUNickName, string FRUPortrait,string RUPortrait,string FRUDes,string RUDes)
        {
            SqlParameter[] sqlParams1 = new SqlParameter[6];
            sqlParams1[0] = new SqlParameter("@RUId", RUId);
            sqlParams1[1] = new SqlParameter("@FRUId", FRUId);
            sqlParams1[2] = new SqlParameter("@FRURemarkName", FRUNickName);
            sqlParams1[3] = new SqlParameter("@FRUPortrait", FRUPortrait);
            sqlParams1[4] = new SqlParameter("@FRUNickName", FRUNickName);
            sqlParams1[5] = new SqlParameter("@FRUDes", FRUDes);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure, "spAddFriend", sqlParams1);
           
            
           /* SqlParameter[] sqlParams2 = new SqlParameter[6];
            sqlParams2[0] = new SqlParameter("@RUId", FRUId);
            sqlParams2[1] = new SqlParameter("@FRUId", RUId);
            sqlParams2[2] = new SqlParameter("@FRURemarkName", RUNickName);
            sqlParams2[3] = new SqlParameter("@FRUPortrait", RUPortrait);
            sqlParams2[4] = new SqlParameter("@FRUNickName", RUNickName);
            sqlParams1[5] = new SqlParameter("@FRUDes", RUDes);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure, "spAddFriend", sqlParams2);*/
            
            return 1;
        }

        //获取好友备注名称
        public string GetFriendRemark(string RUId, ArrayList arrFRUIds, ArrayList arrFRURemarkNames, ArrayList arrFRUNickNames)
        {
            arrFRUIds.Clear();
            arrFRUNickNames.Clear();
            arrFRURemarkNames.Clear();
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border='0px' style='width:100%;border:1px solid #CCCCCC;' cellPadding='0' cellSpacing='2'>");
            sb.Append("<tr><td style='height:25px;background:#CCCCCC'>好友ID</td><td style='height:25px;background:#CCCCCC'>昵称</td><td style='height:25px;background:#CCCCCC'>备注名称</td></tr>");
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT [FRUId],[FRURemarkName],[FRUNickName] FROM [FriendList] WHERE  [RUId]=" + RUId);
            while (dr.Read())
            {
                Encoding edUTF = Encoding.UTF8;
                Encoding edGB = Encoding.GetEncoding("GB2312");
                sb.Append("<tr>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["FRUId"].ToString() + "</td>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["FRUNickName"].ToString() + "</td>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["FRURemarkName"].ToString() + "</td>" + "</tr>");
                arrFRUIds.Add(dr["FRUId"].ToString().Trim());
                arrFRURemarkNames.Add(dr["FRURemarkName"].ToString().Trim());
                arrFRUNickNames.Add(dr["FRUNickName"].ToString().Trim());

            }
            sb.Append("</table>");
            dr.Close();
            ConnSql.Close();
            return sb.ToString();
        }
         
         //修改相应好友备注名称
        public int EditFriendRemark(string RUId,string FRUId,string FRURemarkName)
        {
            SqlParameter[] sqlParams1 = new SqlParameter[3];
            sqlParams1[0] = new SqlParameter("@RUId", RUId);
            sqlParams1[1] = new SqlParameter("@FRUID", FRUId);
            sqlParams1[2] = new SqlParameter("@FRURemarkName", FRURemarkName);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure, "spModifyFriendRemarkname", sqlParams1);

            return 1;
        }

         //获取好友 "删除"用列表
        public string GetFriend(string RUId, ArrayList arrFRUIds, ArrayList arrFRURemarkNames) 
        {
            arrFRUIds.Clear();
            arrFRURemarkNames.Clear();
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border='0px' style='width:100%;border:1px solid #CCCCCC;' cellPadding='0' cellSpacing='2'>");
            sb.Append("<tr><td style='height:25px;background:#CCCCCC'>好友ID</td><td style='height:25px;background:#CCCCCC'>备注名称</td></tr>");
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT [FRUId],[FRURemarkName] FROM [FriendList] WHERE  [RUId]=" + RUId);
            while (dr.Read())
            {
                Encoding edUTF = Encoding.UTF8;
                Encoding edGB = Encoding.GetEncoding("GB2312");
                sb.Append("<tr>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["FRUId"].ToString() + "</td>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["FRURemarkName"].ToString() + "</td>" + "</tr>");
                arrFRUIds.Add(dr["FRUId"].ToString().Trim());
                arrFRURemarkNames.Add(dr["FRURemarkName"].ToString().Trim());
                
            }
            sb.Append("</table>");
            dr.Close();
            ConnSql.Close();
            return sb.ToString();
        }
         //获取好友信息  加入群时用
        public string GetFriendForGroup(string RUId, ArrayList arrFRUIds, ArrayList arrFRUNickNames)
        {
            arrFRUIds.Clear();
            arrFRUNickNames.Clear();
            StringBuilder sb = new StringBuilder();
            sb.Append("<table border='0px' style='width:100%;border:1px solid #CCCCCC;' cellPadding='0' cellSpacing='2'>");
            sb.Append("<tr><td style='height:25px;background:#CCCCCC'>好友ID</td><td style='height:25px;background:#CCCCCC'>昵称</td></tr>");
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT [FRUId],[FRUNickName] FROM [FriendList] WHERE  [RUId]=" + RUId);
            while (dr.Read())
            {
                Encoding edUTF = Encoding.UTF8;
                Encoding edGB = Encoding.GetEncoding("GB2312");
                sb.Append("<tr>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["FRUId"].ToString() + "</td>" + "<td style='height:25px;border-bottom:1px dashed #CCCCCC;'>" + dr["FRUNickName"].ToString() + "</td>" + "</tr>");
                arrFRUIds.Add(dr["FRUId"].ToString().Trim());
                arrFRUNickNames.Add(dr["FRUNickName"].ToString().Trim());

            }
            sb.Append("</table>");
            dr.Close();
            ConnSql.Close();
            return sb.ToString();
        }
        //删除好友
        public int DeleteFriend(string RUId, string FRUId) 
        {
            SqlParameter[] sqlParams1 = new SqlParameter[2];
            sqlParams1[0] = new SqlParameter("@RUId", RUId);
            sqlParams1[1] = new SqlParameter("@FRUId", FRUId);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure, "spDeleteFriend", sqlParams1);
           
            /*SqlParameter[] sqlParams2 = new SqlParameter[2];
           
            sqlParams2[0] = new SqlParameter("@FRUId", FRUId);
            sqlParams2[1] = new SqlParameter("@RUId", RUId);
            SqlHelper.ExecuteNonQuery(ConnSql, CommandType.StoredProcedure, "spDeleteFriend", sqlParams2);*/
            return 1;
        }
    
         //获取好友列表
        public string GetFriendsList(string RUId)
        {
            StringBuilder sb = new StringBuilder();
            SqlDataReader dr = SqlHelper.ExecuteReader(ConnSql, CommandType.Text,
                "SELECT [RUId],[FRUId],[FRUPortrait],[FRURemarkName],[FRUNickName],[FRUDes] FROM [FriendList] WHERE  [RUId]='" + RUId +"'");
            while (dr.Read())
            {
                Encoding edUTF = Encoding.UTF8;
                Encoding edGB = Encoding.GetEncoding("GB2312");
               
                sb.Append("<li id='li" + dr["FRUId"].ToString().Trim() +
                    "' onmouseover='this.style.backgroundColor=\"#F3FBFE\"' onmouseout='this.style.backgroundColor=\"#d6dff7\"'><div class ='intro' id='a"
                    + dr["FRUId"].ToString().Trim() +
                    "' onclick=\"openn(" + dr["FRUId"].ToString() +
                    ",'" + edGB.GetString(Encoding.Convert(edUTF, edGB, edUTF.GetBytes(dr["FRURemarkName"].ToString().Trim()))) +
                    "');\">" + "<div id ='left'>" + "<br>" + "<br>" + "<img vertical-align='center' border='1px' width = '100px' height ='100px' id='img" +
                     "' src='" +
                    dr["FRUPortrait"].ToString().Trim() + "'/>" + " </div>" + "<div id='right'>" + "<br>" + "<br>" + "<br>" + "昵称:" +
                    dr["FRURemarkName"].ToString() + "<br>" + "<br>" + "帐号：" + dr["FRUId"].ToString().Trim() + "<br>" + "<br>" + "<br>" + "签名档:" +
                    dr["FRUDes"].ToString() +"</div>"+
                    "</div>" + dr["FRURemarkName"].ToString().Trim() + "(" + dr["FRUId"].ToString().Trim() +")"+"</li>");
                

               

            }
            dr.Close();
            ConnSql.Close();
            return sb.ToString();
        }
    

         public int GetOnlineFriendsList()
        {
            return 1;
        }


    }
}
